package com.wwg.widget.impl;

import static com.wwg.widget.impl.SpringBoardContentView.FOLDER_ANIMATE_DURATION;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wwg.widget.R;
import com.wwg.widget.fake.FakeBox;
import com.wwg.widget.utils.DefaultAnimationListener;

public class SpringBoardFolderContainer extends FrameLayout {
    private final SpringBoardContentView mContent;
    private final View mBoundary;
    private SpringBoardView.Adapter mAdapter;

    @Nullable
    private Rect mRect;
    private boolean mAnimateShowing;
    private boolean mAnimateHiding;

    public SpringBoardFolderContainer(Context context) {
        this(context, null);
    }

    public SpringBoardFolderContainer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpringBoardFolderContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public SpringBoardFolderContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        View inflate = LayoutInflater.from(context).inflate(
                R.layout.spring_board_folder_view,
                this,
                true);
        mContent = inflate.findViewById(R.id.folder_content);
        mBoundary = inflate.findViewById(R.id.folder_boundary);
        if (mAdapter != null) {
            mContent.setAdapter(mAdapter);
        }

        mAnimateShowing = false;
        mAnimateHiding = false;
    }

    public void setAdapter(@NonNull SpringBoardView.Adapter adapter) {
        mAdapter = adapter;
        if (mContent != null) {
            mContent.setAdapter(adapter);
        }
    }

    public void animateShowContent(Rect rect, Runnable runnable) {
        mContent.requestLayout();
        mAnimateShowing = true;
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setFillEnabled(true);
        animationSet.setFillAfter(true);
        animationSet.setDuration(FOLDER_ANIMATE_DURATION);
        animationSet.setAnimationListener(new DefaultAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(VISIBLE);
                setAlpha(1);
                mAnimateShowing = false;

                runnable.run();
//                mContent.requestLayout();
            }
        });

        mRect = rect;
        int x = rect.left;
        int y = rect.top;

        int measuredWidth = mContent.getMeasuredWidth();
        int measuredHeight = mContent.getMeasuredHeight();

        float scaleX = rect.width() * 1.f / measuredWidth;
        float scaleY = rect.height() * 1.f / measuredHeight;
        animationSet.addAnimation(new ScaleAnimation(scaleX, 1, scaleY, 1));

        float cX = mContent.getX();
        float cY = mContent.getY();
        animationSet.addAnimation(new TranslateAnimation(x - cX, 0, y - cY, 0));
        animationSet.addAnimation(new AlphaAnimation(0, 1));

        mContent.startAnimation(animationSet);
    }

    public void onFolderShow(FakeBox puppet) {
        if ((puppet != null) && (mAdapter != null)) {
            if (mAdapter.onItemInserted(puppet.data)) {
                mContent.insertCurrentBySB();
                mContent.requestLayout();
            }
        }
    }

    public void animateHideContent(Runnable runnable) {
        mAnimateHiding = true;

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setFillEnabled(true);
        animationSet.setFillAfter(true);
        animationSet.setDuration(FOLDER_ANIMATE_DURATION);
        animationSet.setAnimationListener(new DefaultAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(INVISIBLE);
                setAlpha(0);

                postDelayed(() -> {
                    ViewParent parent = getParent();
                    if (parent instanceof ViewGroup) {
                        ((ViewGroup) parent).removeView(SpringBoardFolderContainer.this);
                    }

                    mAnimateHiding = false;

                    mContent.releaseDraggable();
                    runnable.run();
                }, 50);
            }
        });

        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;

        if (mRect != null) {
            x = mRect.left;
            y = mRect.top;
            width = mRect.width();
            height = mRect.height();
        }
        int measuredWidth = mContent.getMeasuredWidth();
        int measuredHeight = mContent.getMeasuredHeight();
        float scaleX = width * 1.f / measuredWidth;
        float scaleY = height * 1.f / measuredHeight;
        animationSet.addAnimation(new ScaleAnimation(1, scaleX, 1, scaleY));
        animationSet.addAnimation(new TranslateAnimation(0, x, 0, y));
        animationSet.addAnimation(new AlphaAnimation(1, 0));

        mContent.startAnimation(animationSet);
    }

    public void setBoundaryVisibility(int visibility) {
        mBoundary.setVisibility(visibility);
    }

    @Nullable
    public SpringBoardContentView getContent() {
        if (mAnimateHiding) {
            return null;
        }

        return mContent;
    }

    public boolean isOperating() {
        return mAnimateHiding || mAnimateShowing;
    }

    public void endTouchEvent(boolean realEnd) {
        mContent.onTouchEventEnd(realEnd);
    }
}
